package com.acetech.admin.web.service;

import com.acetech.admin.web.utils.ResultData;
import com.acetech.admin.web.vo.User;
import com.aliyun.oss.ServiceException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
@Service
public class UploadExcelFileServiceImpl implements UploadExcelFileService {

    @Override
    public ResultData uploadExcel (MultipartFile file) throws ServiceException {
        try {
            String fileName = file.getName();
            InputStream inputStream = file.getInputStream();
            Workbook wb;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = wb.getSheetAt(0);

            // 列数
            int column = sheet.getRow(0).getPhysicalNumberOfCells();
            // 此处可判断Excel列数是否符合要求

            // 行数
            int rows = sheet.getLastRowNum();
            // 此处可判断Excel行数是否符合要求

            List<User> excelData = new ArrayList<>();
            User user;

            // 循环Excel
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }

                user = new User();

                // 用户名
                if (row.getCell(0) != null) {
                    row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                    String userName = row.getCell(0).getStringCellValue();
                    user.setName(userName);
                }
                //添加进list
                excelData.add(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
}
